<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment extends SB_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function save()
	{
		if (!$this->user->id || empty($_POST['text']) || (strlen($_POST['text']) < 2) || empty($_POST['page']))
			die();

		if (!$this->main_model->getArticle($_POST['page']))
			die();

		$comment = empty($_POST['comment'])?0:abs($_POST['comment']);

		$upd = $this->main_model->addComment($_POST['page'], $_POST['text'], $comment);
		$upd['tUserName'] = $this->user->data->pName;

		$upd = array(
			'comment' => (object)$upd,
			'users' => array($this->user->id => $this->user->data->pName)
		);

		$this->load->view('oneComment', $upd);
	}

	function delete($id = 0)
	{
		if (!($id = abs($id)))
			die();
		if (!$this->user->checkRight(7))
		{
			$cdRes = $this->main_model->canDeleteComment($id);
			if ($cdRes !== true)
				die($cdRes);
		}
		die($this->main_model->deleteComment($id)?'1':0);
	}
}
?>